package org.xdemo.example.shiro.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xdemo.example.shiro.entity.Permission;
import org.xdemo.example.shiro.entity.Role;
import org.xdemo.example.shiro.entity.ShiroUser;
import org.xdemo.example.shiro.entity.UserRole;
import org.xdemo.example.websocket.entity.User;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by kz on 2017/8/24.
 */


public class AccountService {

    /****
     * 通过用户名获取用户对象
     *
     * @param username
     * @return
     */
    public ShiroUser getUserByUserName(String username) {
//        ShiroUser user = (User) dao.findObjectByHQL("FROM ShiroUser WHERE username = ?", new Object[] { username });
        ShiroUser user = new ShiroUser();
        user.setUsername(username);
        user.setPassword("123456");
        return user;
    }

    /***
     * 通过用户名获取权限资源
     *
     * @param username
     * @return
     */
    public List<String> getPermissionsByUserName(String username) {
        System.out.println("调用");
        ShiroUser user = getUserByUserName(username);
        if (user == null) {
            return null;
        }
        List<String> list = new ArrayList<String>();
        // System.out.println(user.getUserRoles().get(0).get);
        for (UserRole userRole : user.getUserRoles()) {
            Role role = userRole.getRole();
//            List<Permission> permissions = dao.findAllByHQL("FROM Permission WHERE roleId = ?", new Object[] { role.getId() });
            List<Permission> permissions = new ArrayList<Permission>();
            permissions.add(new Permission());
            for (Permission p : permissions) {
                list.add(p.getUrl());
            }
        }
        return list;
    }

    // 公共的数据库访问接口
    // 这里省略BaseDao dao的编写
//    @Autowired
//    private BaseDao dao;
}
